// 类型判断
var data = {
    // collectlist:[...JSON.parse(localStorage.getItem('collectlist'))],
    collectlist:[],
    shopcartlist: []
}

function reducer(state = data, action) {
    // 进行深拷贝
    var newstate = JSON.parse(JSON.stringify(state))
    // 进行action类型判断
    if (action.type === 'COLLECT') {
        newstate.collectlist.push(action.params)
        // localStorage.setItem('collectlist',JSON.stringify(newstate.collectlist))
        // console.log(newstate.collectlist);
    } else if (action.type === 'CANCELCOLLECT') {
        var index = newstate.collectlist.findIndex((item) => {
            return item.id == action.params.id
        })
        if (index != -1) {
            newstate.collectlist.splice(index, 1);
            // localStorage.setItem('collectlist',JSON.stringify(newstate.collectlist))
        }
    } else if (action.type === 'ADDSHOPCART') {
        var index=newstate.shopcartlist.findIndex((item)=>{
            return item.id==action.params.id
        })
        if(index==-1){
            newstate.shopcartlist.push(action.params)
        }else{
            newstate.shopcartlist.count++
        }
    } else if (action.type === 'SUBSHOPCART') {
        var index=newstate.shopcartlist.findIndex((item)=>{
            return item.id==action.params.id
        })
        if(index==-1){
            newstate.collectlist.splice(action.params);
        }else{
            newstate.shopcartlist.count--
        }
    } else if (action.type === 'COLLECTLIST') {
        newstate.collectlist=action.params
    }else if (action.type === 'SHOPCARTLIST') {
        newstate.shopcartlist=action.params
    }else{
        return state;
    }
    return newstate
}

export default reducer